﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.IO;
using System.Xml;

using Components.Base.ComponentInterface;

//includes for seans graphics compoents
using SunXNA.RainbowXNA;
using SunXNA;
using NeedsMoreNinja.EventManager;

//
using Sound;
using Input;

namespace Components.SplashScreen
{
    class SplashScreen : ComponentInterface
    {
        CRainbowXNA.TextureObjectHandleType m_Background;//handle for the textureObject
        CRainbowXNA.TextureObjectHandleType m_Layer;//handle for the textureObject

        private Vector2 m_BackgroundPos = new Vector2(0, 0);
        private Rectangle m_BackgroundRect = new Rectangle(0, 0, 1280, 720);

        //get initialised by xml
        private Vector2 m_LayerPosition;
        private Rectangle m_LayerRect = new Rectangle(0,0,512,64);

        private Boolean m_LayerShown = false;

        private double m_LastUpdate = 0;

        private double m_FlashTime = 0;
        
        public override void draw2D()
        {
            //render background
            Singleton<CRainbowXNA>.Instance.RenderTexture(m_Background, ref m_BackgroundPos, ref m_BackgroundRect);

            if (m_LayerShown)
            {
                //render extra layer
                Singleton<CRainbowXNA>.Instance.RenderTexture(m_Layer, ref m_LayerPosition, ref m_LayerRect);
            }
        }

        public override void draw3D()
        {

        }

        public override void update(GameTime time)
        {
            if (time.TotalGameTime.TotalSeconds - m_LastUpdate > m_FlashTime)
            {
                if (m_LayerShown)
                {
                    m_LayerShown = false;
                }
                else
                {
                    m_LayerShown = true;
                }

                m_LastUpdate = time.TotalGameTime.TotalSeconds;
            }
        }

        public override void loadContent(ContentManager content, String xmlFile)
        {
            StreamReader reader = new StreamReader(xmlFile);
            XmlDocument machineState = new XmlDocument();
            machineState.LoadXml(reader.ReadToEnd());
            reader.Close();

            //now start reading from the xml
            XmlNode root = machineState.SelectSingleNode("SplashScreen");

            //gets all the nodes
            XmlNodeList nodes = root.ChildNodes;
            m_Background = Singleton<SunXNA.RainbowXNA.CRainbowXNA>.Instance.LoadTexture(nodes[0].FirstChild.InnerText.Trim());

            //now we do the layer
            XmlNodeList children = nodes[2].ChildNodes;

            m_Layer = Singleton<SunXNA.RainbowXNA.CRainbowXNA>.Instance.LoadTexture(children[0].FirstChild.InnerText.Trim());
            m_FlashTime = Convert.ToDouble(children[1].FirstChild.InnerText.Trim());

            m_LayerPosition.X = Convert.ToInt32(children[2].FirstChild.InnerText.Trim());
            m_LayerPosition.Y = Convert.ToInt32(children[3].FirstChild.InnerText.Trim());
        
            //now we finally do the music

            nodes[3].FirstChild.InnerText.Trim();
        }

        public override void unloadContent(ContentManager content)
        {

        }
    }
}
